package com.example.huanhang.changzhouapp.util;

import android.app.DatePickerDialog;
import android.content.Context;
import android.widget.TextView;

import com.allen.library.SuperTextView;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;


public class DateUtils {

    /**
     * 获取当前时间
     *
     * @return long
     */
    public static long getCurrentTime() {

        return System.currentTimeMillis();
    }

    /**
     * @return 获取当前日期
     */
    public static String getCurrentDateJ() {

        return getCurrentYear() + "-" + getCurrentMonth() + "-" + getCurrentDay();
    }

    /**
     * @return 获取当前日期
     */
    public static String getCurrentDateHanZi() {

        return getCurrentYear() + "年" + getCurrentMonth() + "月" + getCurrentDay() + "日";
    }

    /**
     * 获取当前日期和时间
     */
    public static String getCurrentDateHms() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("GMT+08"));
        return format.format(new Date());

    }

    /**
     * 获取一个月前的日期
     */
    public static String getMonthAgo() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat().parse(getCurrentDateJ()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.MONTH, -1);
        return simpleDateFormat.format(calendar.getTime());
    }

    public static String getCurrentDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat().parse(getCurrentDateJ()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * @return 获取当年
     */
    public static int getCurrentYear() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        return Integer.parseInt(format.format(getCurrentTime()));
    }

    /**
     * @return 获取当月
     */
    public static int getCurrentMonth() {
        SimpleDateFormat format = new SimpleDateFormat("MM");
        return Integer.parseInt(format.format(getCurrentTime()));
    }

    /**
     * @return 获取当天
     */
    public static int getCurrentDay() {
        SimpleDateFormat format = new SimpleDateFormat("dd");
        return Integer.parseInt(format.format(getCurrentTime()));
    }

    //获取当前小时
    public static int getHour() {
        SimpleDateFormat format = new SimpleDateFormat("HH");
        return Integer.parseInt(format.format(getCurrentTime()));
    }

    //获取当前分钟
    public static int getMinute() {
        SimpleDateFormat format = new SimpleDateFormat("mm");
        return Integer.parseInt(format.format(getCurrentTime()));
    }

    /**
     * @return
     */
    public static int getMonth() {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH);
        return month;
    }

    /**
     * @param context
     * @return 返回数据
     */
    public static void showCalendarDialog(final Context context, final TextView et_date) {

        new DatePickerDialog(context, 0, (datePicker, i, i1, i2) -> {
            String mouth = isOneLengthMouth(i1 + 1);
            String day = isOneLengthDay(i2);
            et_date.setText(i + "-" + mouth + "-" + day);
        }, getCurrentYear(), getMonth(), getCurrentDay()).show();
    }

    public static String isOneLengthMouth(int s) {
        String value;
        if (String.valueOf(s).length() == 1) {
            value = "0" + (s);
        } else {
            value = "" + (s);
        }
        return value;
    }

    public static String isOneLengthDay(int s) {
        String value;
        if (String.valueOf(s).length() == 1) {
            value = "0" + (s);
        } else {
            value = "" + (s);
        }
        return value;
    }

    /**
     * @param context
     * @return 返回数据
     */
    public static void showSuperTextCalendar(final Context context, final SuperTextView et_date) {

        new DatePickerDialog(context, 0, (datePicker, i, i1, i2) -> {
            String mouth = isOneLengthMouth(i1 + 1);
            String day = isOneLengthDay(i2);
            String date = i + "-" + mouth + "-" + day + "  ";
            et_date.setRightString(date + getWeek(date));
        }, getCurrentYear(), getMonth(), getCurrentDay()).show();
    }

    /**
     * 将日期字符串转成日期
     *
     * @param strDate 字符串日期
     * @return java.util.date日期类型
     */

    public static Date parseDate(String strDate) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date returnDate = null;
        try {
            returnDate = dateFormat.parse(strDate);
        } catch (ParseException ignored) {

        }
        return returnDate;

    }

    //获取当前日期的前一天
    public static String oneDateBefore() {
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance();  //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
        dBefore = calendar.getTime();   //得到前一天的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        return sdf.format(dBefore);
    }

    //获取当前日期的hou一天
    public static String oneDateAfter() {
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance();  //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, +1);  //设置为hou一天
        dBefore = calendar.getTime();   //得到hou一天的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        return sdf.format(dBefore);
    }

    /**
     * 获取日期的前一天后一天
     *
     * @param currentTime
     * @param i           -1  前一天  +1  后一天
     * @return
     */
    public static String getQianAndHouDate(String currentTime, int i) {
        Date date = parseDate(currentTime);
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance();  //得到日历
        calendar.setTime(date);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, i);  //设置为前一天
        dBefore = calendar.getTime();   //得到前一天的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        return sdf.format(dBefore);
    }

    /**
     * 获取周几
     *
     * @param date
     * @return
     */
    public static String getWeek(String date) {
        String Week = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            Week += "星期天";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            Week += "星期一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
            Week += "星期二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
            Week += "星期三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
            Week += "星期四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
            Week += "星期五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            Week += "星期六";
        }
        return Week;

    }

    /**
     * 获取指定网站的日期时间
     */
    public static String getWebSiteDatetime() {
        try {
            String webUrl = "http://www.taobao.com";//淘宝
            URL url = new URL(webUrl);// 取得资源对象
            URLConnection uc = url.openConnection();// 生成连接对象
            uc.connect();// 发出连接
            long ld = uc.getDate();// 读取网站日期时间
            Date date = new Date(ld);// 转换为标准时间对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);// 输出北京时间
            return sdf.format(date);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static List<String> findDates(Date dBegin, Date dEnd) {
        List<String> lDate = new ArrayList<>();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        lDate.add(sd.format(dBegin));
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(sd.format(calBegin.getTime()));
        }
        return lDate;
    }

}